package com.zuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.Doc;
import com.zuo.entity.User;
import com.zuo.service.DocService;
import com.zuo.util.HrmConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/3 11:27
 */
@Controller
public class DocController {

    @Autowired
    private DocService docService;


    @RequestMapping("/document")
    public String doc(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page, HrmConstants.PAGE_DEFAULT_SIZE);

        List<Doc> docs = docService.listDoc();

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Doc> pageInfo = new PageInfo<>(docs);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("docs",docs);
        model.addAttribute("type","all");
        return "document/document.jsp";
    }

    @RequestMapping("/searchDoc")
    public String searchDoc(String dtitle,Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<Doc> docs = docService.searchDoc(dtitle);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Doc> pageInfo = new PageInfo<>(docs);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("docs",docs);
        model.addAttribute("type","search");
        model.addAttribute("dtitle",dtitle);
        return "document/document.jsp";
    }

    @RequestMapping("/toAddDoc")
    public String toAddDoc(){
        return "document/showAddDocument.jsp";
    }

    @RequestMapping("/addDoc")
    public String upload(Doc doc, @RequestParam(value = "file",required = true) MultipartFile file,Model model) throws IOException {
        File f = new File(HrmConstants.FILES+file.getOriginalFilename());
        if(!StringUtils.isNotBlank(file.getOriginalFilename())){
            return "forward:toAddDoc.action";
        }
        file.transferTo(f);
        doc.setDpath(file.getOriginalFilename());
        int result = docService.upload(doc);
        if(result!=0){
            model.addAttribute("info","上传成功");
        }else{
            model.addAttribute("info","上传出错");
        }
        return "forward:document.action";
    }

    @RequestMapping("/toUpdateDoc")
    public String toUpdateDoc(int id,Model model){
        Doc doc = docService.getOne(id);
        model.addAttribute("doc",doc);
        return "document/showUpdateDocument.jsp";
    }

    @RequestMapping("/updateDoc")
    public String updateDoc(Doc doc, @RequestParam(value = "file",required = true) MultipartFile file,Model model) throws IOException {
        File f = new File(HrmConstants.FILES+file.getOriginalFilename());
        if(!StringUtils.isNotBlank(file.getOriginalFilename())){
            return "forward:document.action";
        }
        file.transferTo(f);
        doc.setDpath(file.getOriginalFilename());
        int result = docService.updateDoc(doc);
        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:document.action";
    }

    @RequestMapping("/downloadDoc")
    public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("id") Integer id, Model model) throws IOException {
        Doc doc = docService.getOne(id);

        File file = new File(HrmConstants.FILES+doc.getDpath());//新建一个文件

        HttpHeaders headers = new HttpHeaders();//http头信息

        String downloadFileName = new String(doc.getDpath().getBytes("UTF-8"),"iso-8859-1");//设置编码

        headers.setContentDispositionFormData("attachment", downloadFileName);

        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        //MediaType:互联网媒介类型  contentType：具体请求中的媒体类型信息
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }

    @RequestMapping("/deleteDoc")
    public String deleteDoc(String ids,Model model){
        int result = docService.deleteDocs(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:document.action";
    }


}
